/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package doan2database;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
 *
 * @author DongVan
 */
public class TuyenTau {
        private int m_IDTuyenTau;
	private String m_DiaDiemDi;
	private String m_DiaDiemDen;
	private String m_GioDi;
	private String m_GioDen;
	private String m_GiaVe;
	private int m_NguoiLap;
	public int getM_IDTuyenTau() {
		return m_IDTuyenTau;
	}
	public void setM_IDTuyenTau(int m_IDTuyenTau) {
		this.m_IDTuyenTau = m_IDTuyenTau;
	}
	public String getM_DiaDiemDi() {
		return m_DiaDiemDi;
	}
	public void setM_DiaDiemDi(String m_DiaDiemDi) {
		this.m_DiaDiemDi = m_DiaDiemDi;
	}
	public String getM_DiaDiemDen() {
		return m_DiaDiemDen;
	}
	public void setM_DiaDiemDen(String m_DiaDiemDen) {
		this.m_DiaDiemDen = m_DiaDiemDen;
	}
	public String getM_GioDi() {
		return m_GioDi;
	}
	public void setM_GioDi(String m_GioDi) {
		this.m_GioDi = m_GioDi;
	}
	public String getM_GioDen() {
		return m_GioDen;
	}
	public void setM_GioDen(String m_GioDen) {
		this.m_GioDen = m_GioDen;
	}
	public String getM_GiaVe() {
		return m_GiaVe;
	}
	public void setM_GiaVe(String m_GiaVe) {
		this.m_GiaVe = m_GiaVe;
	}
	public int getM_NguoiLap() {
		return m_NguoiLap;
	}
	public void setM_NguoiLap(int m_NguoiLap) {
		this.m_NguoiLap = m_NguoiLap;
	}
	public TuyenTau(int m_IDTuyenTau, String m_DiaDiemDi, String m_DiaDiemDen,
			String m_GioDi, String m_GioDen, String m_GiaVe, int m_NguoiLap) {
		this.m_IDTuyenTau = m_IDTuyenTau;
		this.m_DiaDiemDi = m_DiaDiemDi;
		this.m_DiaDiemDen = m_DiaDiemDen;
		this.m_GioDi = m_GioDi;
		this.m_GioDen = m_GioDen;
		this.m_GiaVe = m_GiaVe;
		this.m_NguoiLap = m_NguoiLap;
	}
	public TuyenTau(){
		
	}
	
	
	
	AESEncrypt aes = new AESEncrypt();
	Datapro dataprovider = new Datapro();

	
	public ArrayList<TuyenTau> selected() {
		String sqlQuery = "select * from TUYEN_TAU";
		ArrayList<TuyenTau> listTuyenTau = new ArrayList<TuyenTau>();
		ResultSet rs = dataprovider.Selected(sqlQuery);
		try {
			while (rs.next()) {
				TuyenTau tuyenTau = new TuyenTau();
				tuyenTau.m_IDTuyenTau = rs.getInt(1);
				tuyenTau.m_DiaDiemDi = aes.decryptString(rs.getString(2));
				tuyenTau.m_DiaDiemDen = aes.decryptString(rs.getString(3));
				tuyenTau.m_GioDi = aes.decryptString(rs.getString(4));
				tuyenTau.m_GioDen = aes.decryptString(rs.getString(5));
				tuyenTau.m_GiaVe = aes.decryptString(rs.getString(6));
				tuyenTau.m_NguoiLap = rs.getInt(7);
				listTuyenTau.add(tuyenTau);
			}
			dataprovider.disconnect();
	        rs.close();
	        return listTuyenTau;
	        
	    } catch ( SQLException ex) {
	        Logger.getLogger(Datapro.class.getName()).log(Level.SEVERE, null, ex);
	    }
		return null;
	}
	

	public boolean insert() {
		String diaDiemDi = aes.encryptString(this.m_DiaDiemDi);
		String diaDiemDen = aes.encryptString(this.m_DiaDiemDen);
		String gioDi = aes.encryptString(this.m_GioDi);
		String gioDen = aes.encryptString(this.m_GioDen);
		String giaVe = aes.encryptString(this.m_GiaVe);
		int nguoiLap = this.m_NguoiLap;
		String query =  "insert into TUYEN_TAU values('"+ diaDiemDi + "', '"+ diaDiemDen +"', '"+ gioDi +"', '"+ gioDen +"', '"+ giaVe +"'," +nguoiLap+ "  )";
		boolean result =  dataprovider.Insert(query);
		dataprovider.disconnect();
		return result;
	}
	
	public boolean update(){
		int idTuyenTau = this.m_IDTuyenTau;
		String diaDiemDi = aes.encryptString(this.m_DiaDiemDi);
		String diaDiemDen = aes.encryptString(this.m_DiaDiemDen);
		String gioDi = aes.encryptString(this.m_GioDi);
		String gioDen = aes.encryptString(this.m_GioDen);
		String giaVe = aes.encryptString(this.m_GiaVe);
		int nguoiLap = this.m_NguoiLap;
		String query = "update TUYEN_TAU set DiaDiemDi = '"+ diaDiemDi +"', DiaDiemDen = '"+diaDiemDen+"', GioDi = '"+gioDi+"', GioDen = '"+gioDen+"', GiaVe = '"+giaVe+"',NguoiLap = "+nguoiLap+", where IDTuyenTau = " + idTuyenTau + "\"";
		boolean result = dataprovider.Update(query);
		dataprovider.disconnect();
		return result;
	}
	
	public boolean delete(){
		String query = "delete from TUYEN_TAU where = " + this.m_IDTuyenTau + "\"";
		boolean result = dataprovider.Delete(query);
		dataprovider.disconnect();
		return result;
	}
}
